package com.test.daily.leetcode.y2022.m01.day0110;

/**
 * IsAdditiveNumber
 *
 * @author Tom on 2022/1/10
 */
public class Code01_IsAdditiveNumber {
    public boolean isAdditiveNumber(String num){
        int n = num.length();
        for (int secondStart = 0; secondStart < n-1; secondStart++) {
            if(num.charAt(0) == '0' && secondStart != 1){
                break;
            }
            for(int secondEnd = secondStart; secondEnd < n -1; ++secondEnd){
                if(num.charAt(secondEnd) == '0' && secondStart != secondEnd){
                    break;
                }
                if(valid(secondStart, secondEnd,num)){
                    return true;
                }
            }
        }
        return false;
    }
    public boolean valid(int secondStart, int secondEnd, String num){
        int n = num.length();
        int firstStart = 0, firstEnd = secondStart - 1;
        while(secondEnd <= n -1){
            String third = stringAdd(num, firstStart, firstEnd, secondStart, secondEnd);
            int thirdStart = secondEnd + 1;
            int thirdEnd = secondEnd +third.length();
            if(thirdEnd >= n || !num.substring(thirdStart, thirdEnd + 1).equals(third)){
                break;
            }
            if(thirdEnd == n - 1){
                return true;
            }
            firstStart = secondStart;
            firstEnd= secondEnd;
            secondStart = thirdStart;
            secondEnd  = thirdEnd;
        }
        return false;
    }
    public String stringAdd(String s, int firstStart, int firstEnd, int secondStart, int secondEnd){
        StringBuffer third = new StringBuffer();
        int carry = 0, cur = 0;
        return "";
    }
}
